import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_dk_form/flutter_dk_form.dart';
import 'package:flutter_dynamic_forms/flutter_dynamic_forms.dart';
import 'text_input.dart' as model;

class TextInputRenderer extends FormElementRenderer<model.DyTextInput> {
  @override
  Widget render(
      model.DyTextInput element,
      BuildContext context,
      FormElementEventDispatcherFunction dispatcher,
      FormElementRendererFunction renderer) {
    void onChangedHandler(String value) {
      print("onChangedHandler ${element.id} $value");
      dispatcher(ChangeValueEvent(value: value,elementId: element.id));
    }

    return DkTextFormField(
      initialValue: element.value,
      attribute: element.id,
      decoration: InputDecoration(
        labelText: element.label,
      ),
      validator: (val){

        var errorText = element.validations
            .firstWhere((v) => !v.isValid, orElse: () => null)
            ?.message;
        print('errorText=$errorText');
        return errorText;
      },
      onChanged: onChangedHandler,
    );
  }
}
